package com.chey.runner;

import com.chey.listener.FileListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @author JanYao
 * @version 0.1
 */
@Component
@Slf4j
public class FileRunner  implements ApplicationRunner {
    private String filePath = "D:\\files";
    @Override
    public void run(ApplicationArguments args){
        try {
            FileAlterationObserver fileAlterationObserver = new FileAlterationObserver(new File(filePath));
            FileListener fileListener = new FileListener();

            fileAlterationObserver.addListener(fileListener);
            FileAlterationMonitor fileAlterationMonitor = new FileAlterationMonitor(2000, new FileAlterationObserver[]{fileAlterationObserver});

            fileAlterationMonitor.start();
        } catch (Exception e) {
            log.info("监听器启动失败");
            e.printStackTrace();
        }
    }
}
